//
//  ViewController.swift
//  XNavigator
//
//  Created by anru on 09/01/2024.
//  Copyright (c) 2024 anru. All rights reserved.
//

import UIKit
import XNavigator




class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        makeUI()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    let tableView = UITableView()
    let datas = ["A", "B", "C", "D","Alert", "web", "URL", "URL 2"]
    
 
    
    
    func makeUI(){
        self.view.addSubview(self.tableView)
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.frame = view.bounds
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        
        route.register("navigator://user/[name]") { url, values, context in
            guard let username = values["name"] as? String else { return nil }
            return D_VController(name: username)
          }
    }
}

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")
        guard let cell = cell else { return UITableViewCell(style: .subtitle, reuseIdentifier: "UITableViewCell") }
        
        cell.textLabel?.text = datas[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
}

extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch datas[indexPath.row] {
        case "A":
            route.navigate(to: .ARoute)
        case "B":
            route.navigate(to: .Boute)
        case "C":
            route.navigate(to: .CRoute)
        case "D":
            route.navigate(to: .DRoute)
        case "Alert":
            route.navigate(to: .alert(title: "提示", message: "测试Custom"))
        case "web":
            route.navigate(to: .webRoute(url: URL.init(string: "www.baidu.com")!), animated: true)
        case "URL":
            route.navigate("navigator://user/apple")
        case "URL 2":
            route.navigate(to: "/user")
        default: break
            
        }
    }
}
